package com.xuecheng.content.service.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.extra.template.engine.beetl.BeetlEngine;
import com.alibaba.fastjson.JSON;
import com.xuecheng.basic.api.BasicClient;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.CategoryVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.TeachplanVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.config.MinioTemplate;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.CoursePub;
import com.xuecheng.content.mapper.CoursePubMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CoursePubService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.service.PubPageService;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.web.exception.BusinessException;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 课程发布 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-30
 */
@Service
public class CoursePubServiceImpl extends ServiceImpl<CoursePubMapper, CoursePub> implements CoursePubService {

    @Autowired
    private TeachplanService teachplanService;

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private BasicClient basicClient;

    @Autowired
    private PubPageService pubPageService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 课程预览
    @Override
    public Map preview(Long courseBaseId) {
        //课程计划
        ResponseResult<TeachplanVo> result = teachplanService.treeNodes(courseBaseId);
        TeachplanVo data = result.getData();
        // 课程发布
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        CoursePub coursePub = BeanHelper.copyProperties(courseBase, CoursePub.class);
        // 设置课程分类名
        String mtName = findByCategoryName(coursePub.getMt());
        coursePub.setMtName(mtName);
        String stName = findByCategoryName(coursePub.getSt());
        coursePub.setStName(stName);
        // 课程计划JSon
        coursePub.setTeachplan(JSON.toJSONString(data));
        // 收费规则
        coursePub.setPrice(courseBase.getPrice().longValue());
        //填充模型
        Map model = new HashMap();
        model.put("teachplanNode", data);
        model.put("coursePub", coursePub);
        return model;


    }
    // 课程发布
    @Override
    @Transactional
    public ResponseResult kechengfabu(Long courseBaseId) throws IOException, TemplateException {
        //查询课程的信息
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        // 校验状态
        String status = courseBase.getAuditStatus(); // 得出状态
        if (!status.equals(CourseConstants.AUDIT_PASTED_STATUS)){//如果不是已经审核通过的 就不可以发布
            throw  new BusinessException(ErrorCode.COURSEPUBSHRRROR);
        }
        //查询coursePub
        Map map = this.preview(courseBaseId);
        CoursePub coursePub = (CoursePub) map.get("coursePub");
        //在设置一次课程id
        coursePub.setCourseId(courseBaseId);
        // 新增
        this.saveOrUpdate(coursePub);

        // 修改课程状态
        courseBase.setAuditStatus(CourseConstants.AUDIT_PUBLISHED_STATUS);
        courseBaseService.updateById(courseBase);


        // 6.调用多线程技术，生成html，保存minio
        pubPageService.pubPage(map, coursePub.getId());



        // 7. 发送mq消息
        rabbitTemplate.convertAndSend("content-exchange","course.pub" , coursePub.getId());



        // 返回结果
        return ResponseResult.okResult();
    }











    // 设置课程分类名
    private String findByCategoryName(String mt){
        ResponseResult<CategoryVo> byId = basicClient.findById(mt);
        CategoryVo data = byId.getData();
        String name = data.getName();
        return name;
    }


}
